package com.xiaoquan.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xiaoquan.common.exception.BizCodeEnums;
import com.xiaoquan.gulimall.member.exception.PhoneExistException;
import com.xiaoquan.gulimall.member.exception.UsernameExistException;
import com.xiaoquan.gulimall.member.feign.CouponFeignService;
import com.xiaoquan.gulimall.member.vo.MemberLoginVo;
import com.xiaoquan.gulimall.member.vo.MemberRegisterVo;
import com.xiaoquan.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiaoquan.gulimall.member.entity.MemberEntity;
import com.xiaoquan.gulimall.member.service.MemberService;
import com.xiaoquan.common.utils.PageUtils;
import com.xiaoquan.common.utils.R;



/**
 * 会员
 *
 * @author xiaoquan
 * @email 1461240780@qq.com
 * @date 2021-06-03 20:35:32
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("xiaoquan");
        R membercoupons = couponFeignService.membercoupons();

        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }

    /**
     * 会员注册
     * @return
     */
    @RequestMapping("/register") // 微服务之间都是 http + json 数据 所以要加@RequestBody 将数据转换成对象MemberRegisterVo
    public R register(@RequestBody MemberRegisterVo vo){
        try { // 注册可能会有很多异常
            memberService.regist(vo);
        }catch (UsernameExistException usernameExistException){
            return R.error(BizCodeEnums.USERNAME_EXIST_EXCEPTION.getCode(), BizCodeEnums.USERNAME_EXIST_EXCEPTION.getMsg());
        }catch (PhoneExistException phoneExistException){
            return R.error(BizCodeEnums.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnums.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (Exception e){
            return R.error();
        }
        return R.ok();
    }

    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity =  memberService.login(socialUser);
        if (memberEntity != null){
            // 登录成功 应该将用户所有的信息返回
            return R.ok().setData(memberEntity);
        }else {
            // 登录失败
            return R.error(BizCodeEnums.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnums.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }


    /**
     * 会员登录
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity memberEntity =  memberService.login(vo);
        if (memberEntity != null){
            // 登录成功 应该将用户所有的信息返回
            return R.ok().setData(memberEntity);
        }else {
            // 登录失败
            return R.error(BizCodeEnums.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnums.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
